<template>
	<view class="drawbox flexcolumn">
		<view class="flexcolumn gginner" :style="'background-image: url(' + gginner_bg+ ')'">
			<view style="margin-bottom:30rpx;" class="c_000 f16 textcenter mt_10 mb_25">做任务赢取抽奖次数</view>
			<!-- 图片 -->
			<view class="drawimg flexcenter">
				<ad ad-type="banner" style="width:400rpx;heiht:270rpx" unit-id="adunit-fd4e9b20fb29be8c"
					ad-interval="30" @load="onAdLoad" @error="onAdError" @close="onAdClose" @rewarded="onAdRewarded">
				</ad>
			</view>
			<view v-if="advert.data.adv_type == 1 || advert.data.adv_type == 2" class="drawimg flexcenter">

				<ad ad-type="banner" style="width:400rpx;heiht:270rpx" unit-id="adunit-fd4e9b20fb29be8c"
					ad-interval="30" @load="onAdLoad" @error="onAdError" @close="onAdClose" @rewarded="onAdRewarded">
				</ad>
				<!-- <view class="tips">广告</view>
			adunit-cffb55b2ad798f78     视频
			adunit-fd4e9b20fb29be8c     banner
			adunit-54c4a06c561e267f     激励
			<image :src="advert.data ? domain + advert.data.url : ''" alt="" @click.stop="handleTask" /> -->
			</view>
			<!-- 视频 -->
			<ad v-else style="width:400rpx;heiht:270rpx" unit-id="adunit-cffb55b2ad798f78" ad-type="video"
				@load="onAdLoad" @error="onAdError" @close="onAdClose" @rewarded="onAdRewarded">
			</ad>

			<view class="flexbetween">
				<view v-if="advert.data.is_force != 1"
					class="flexcenter detailbtn detailbtn3 mauto mt_24 mb_10 c_fff f16" @click.stop="closePop">
					返回抽奖
				</view>
				<template v-if="advert.data.adv_type == 3">
					<!--  -->
					<view v-if="disableval" class="flexcenter detailbtn detailbtn3 mauto mt_24 mb_10 c_fff f14">
						{{ djs_time }} s后再抽一次
					</view>
					<view v-else class="flexcenter detailbtn detailbtn3 mauto mt_24 mb_10 c_fff f16"
						@click.stop="closePop">再抽一次</view>
				</template>
				<template v-else>
					<view style="width:100%" class="flexcenter detailbtn detailbtn3 mauto mt_24 mb_10 c_fff f16"
						@click.stop="doTask">
						做任务
					</view>
				</template>
			</view>
		</view>
	</view>
</template>

<script>
	//import playVideo from '../playVideo.vue'
	export default {
		data() {
			return {
				gginner_bg: `${uni.$http.baseUrl}/upload/we/common/gj_bg.png`,
				imgPath: uni.$http.baseUrl + "/",
				djs_time: 0,
				disableval: false,

			};
		},
		components: {
			//	playVideo
			//VideoAdv
		},
		props: {
			type: {
				default: 4,
			},
			advert: {
				default: null,
			},
		},
		mounted() {

			this.rewardedVideoAd = uni.createRewardedVideoAd({
				adUnitId: 'adunit-54c4a06c561e267f' // 替换为你的激励视频广告位ID
			})

			// 监听激励视频广告加载成功事件
			this.rewardedVideoAd.onLoad(() => {


			})

			// 监听激励视频广告关闭事件
			this.rewardedVideoAd.onClose((res) => {
				if (res.isEnded) {
					// 用户观看完整广告
					this.handleTask();
					console.log('用户观看完整广告')
					this.rewardedVideoAd.offClose() // 移除关闭事件监听
					this.rewardedVideoAd.destroy()
				} else {
					// 用户未观看完整广告
					console.log('用户未观看完整广告')
				}
			})

			// 监听激励视频广告错误事件
			this.rewardedVideoAd.onError((err) => {
				console.log('激励视频广告错误', err)
			})
		},
		created() {
			console.log('11111', this.advert)
			if (this.advert.data.adv_type == 3) {
				console.log('2222', this.advert.data.is_limit)
				if (this.advert.data.is_limit == 1) {
					this.disableval = true;
					this.djs_time = this.advert.data.time;
					console.log('3333', this.disableval)
					this.djsSetTime()
				} else {
					console.log('4444', this.advert.data.is_limit)
					this.disableval = false;
				}
				if (this.advert.data.is_force != 1) {
					console.log('555', this.advert.data.is_limit)
					this.handleTask()
				}
			}
		},
		methods: {
			doTask() {
				this.rewardedVideoAd.show()
			},
			onAdLoad() {
				//this.handleTask();
			},
			onAdError(err) {
				console.log('激励广告加载失败', err)
			},
			onAdClose() {
				//this.handleTask();
				console.log('激励广告关闭')
			},
			onAdRewarded() {
				if (e.isEnded) {
					this.handleTask();
					// 用户观看完整广告，给予奖励
					// 发放奖励的逻辑代码
				} else {
					// 用户未观看完整广告，不给予奖励
					// 其他逻辑处理
				}
			},

			// 关注公众号
			goGzh() {},

			async handleTask(type) {
				let task = JSON.parse(uni.getStorageSync('taskInfo', 'session')).task;
				if (this.advert.data) {

					var res = await uni.$http.post("/wxuser/index/advertRecord", {
						id: task.id
					})
					uni.showToast({
						title: "获取奖励成功",
						icon: "success"
					})
					if (type == 1) {
						console.log(this.advert.data.link)
					};
				}
			},

			// 观看视频15秒倒计时
			djsSetTime() {
				this.djs_timer = setInterval(() => {
					this.djs_time--;
					// console.log('this.djs_time:',this.djs_time)
					if (this.djs_time < 1) {
						clearInterval(this.djs_timer);
						this.disableval = false;
						this.handleTask()
						// postBuriedPointRecord(4, this.type, 13, this.time, this.record_id);
					}
				}, 1000);
			},

			goLink() {
				let link = this.advert.data.link;
				if (!link) return;
				if (!isExternal(link)) {
					this.$toast("无效链接");
					return;
				}
				location.href = link;
			},

			// copylink() {
			//   let link = window.location.href;
			//   this.$copyText(link).then(
			//     () => {
			//       let task = JSON.parse(sessionStorage.getItem("taskInfo")).task;
			//       forwardRecord({ id: task.id })
			//       this.closePop()
			//       this.$toast("链接复制成功，请到微信上粘贴");
			//     },
			//     () => {
			//       this.$toast("复制失败，请重新点击");
			//     }
			//   );
			// },

			closePop() {
				this.$emit("close");
			},
		},
	};
</script>

<style lang="scss" scoped>
	.drawbox {
		width: 540rpx;
		padding: 0 40rpx;
		margin: auto;
		//	background-color: #fff;
	}

	.gginner {
		background-size: cover;
		border-radius: 20px;
		padding: 20px;
	}

	.drawimg {
		width: 400rpx;
		height: 265rpx;
		// max-height: 6.8rem;
		margin: auto;
		position: relative;
		background-color: #eee;
		border-radius: 10rpx;
		overflow: hidden;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.title {
		color: #76b9fe;
	}

	.detailbtn {
		//   width: 4.16rem;
		height: 1.33rem;
		border-radius: 0.8rem;
		background: #418BF8;
		border: 1px solid #fff;
		line-height: 0;
	}

	.detailbtn2 {
		width: 3.6rem;
		height: 1rem;
	}

	.mauto {
		margin: auto;
	}

	.detailbtn3 {

		margin: 15px 5px;
		width: 320rpx;
		height: 66rpx;
		font-size: 26rpx;
	}

	.tips {
		position: absolute;
		top: 0.2rem;
		right: 0.2rem;
		padding: 5px 12px;
		background-color: rgba(0, 0, 0, 0.5);
		z-index: 9;
		font-size: 14px;
		color: white;
		border-radius: 6px;
	}
</style>